﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace gChartSharp
{
    /// <summary>
    /// http://code.google.com/apis/chart/#axes_label_positions
    /// </summary>
    public class AxisLabelPositionParam : IRenderable 
    {
        /// <summary>
        /// The name of the API
        /// </summary>
        public string Name { get { return "chxp"; } }


        /// <summary>
        /// The label position values
        /// </summary>
        public Dictionary<int, IEnumerable<float>> LabelPositions 
        {
            get { return mLabelPositions ?? (mLabelPositions = new Dictionary<int, IEnumerable<float>>()); }
            set { mLabelPositions = value; }
        }
        private Dictionary<int, IEnumerable<float>> mLabelPositions;


        /// <summary>
        /// Render the API text
        /// </summary>
        /// <returns></returns>
        public string Render()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("&{0}=", Name);

            foreach (KeyValuePair<int, IEnumerable<float>> kv in LabelPositions)
            {
                // only render the KeyValuePair if there is at least one label position in the array
                if (kv.Value.Count() > 0)
                {
                    sb.AppendFormat("{0},", kv.Key);

                    foreach (float labelPosition in kv.Value)
                        sb.AppendFormat("{0},", labelPosition);

                    sb.Remove(sb.ToString().LastIndexOf(','), 1);

                    sb.AppendFormat("{0}", "|");
                }
            }

            sb.Remove(sb.ToString().LastIndexOf('|'), 1);

            return sb.ToString();
        }
    }
}
